package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dinadi.yidi.R;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.http.interfaces.IntegralChangeInterface;
import com.dinadi.yidi.ui.view.ShareMenuPopupWindow;
import com.dinadi.yidi.ui.view.ShowImageWebView;
import com.dinadi.yidi.utils.EmptyUtils;
import com.dinadi.yidi.utils.ImageUtils;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * author: JiaYu.He
 * Date: 2018/5/15  设计师品牌故事
 * Time: 10:47
 */
public class DesignerBrandStoryActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, nick_name, brand_name, signature, tv_follow;
    private ImageView follow, img_designer;
    private ShowImageWebView activity_web;
    private CircleImageView head_img;
    private RelativeLayout layout_brand_story;
    private Button btn_title_right;
    private ShareMenuPopupWindow selectImage;
    private int sharePosition;
    /*判断是否登陆*/
    private boolean loginState;
    private String userId, token, nickName, headImg, dUserId;


    @Override
    protected View getLayout() {
        view = LayoutInflater.from(DesignerBrandStoryActivity.this).inflate(R.layout.activity_designer_brand_story, null);
        return view;
    }

    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected void initView() {
        changePager(2);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        btn_title_right = (Button) view.findViewById(R.id.btn_title_right);
        btn_title_right.setBackgroundResource(R.drawable.product_details_icon_share);
        head_img = (CircleImageView) view.findViewById(R.id.head_img);
        nick_name = (TextView) view.findViewById(R.id.nick_name);
        brand_name = (TextView) view.findViewById(R.id.brand_name);
        signature = (TextView) view.findViewById(R.id.signature);
        layout_brand_story = (RelativeLayout) view.findViewById(R.id.layout_brand_story);
        layout_brand_story.setVisibility(View.GONE);
        tv_follow = (TextView) view.findViewById(R.id.tv_follow);
        tv_follow.setVisibility(View.GONE);
        follow = (ImageView) view.findViewById(R.id.follow);
        follow.setVisibility(View.GONE);
        img_designer = (ImageView) view.findViewById(R.id.img_designer);
        activity_web = (ShowImageWebView) view.findViewById(R.id.activity_web);
     /*   // 支持缩放(适配到当前屏幕)  
        WebSettings webSettings = activity_web.getSettings();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {//少于4.4（不包括4.4）用这个  
            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        }
        //适应屏幕，大于等于4.4用这个  
        webSettings.setUseWideViewPort(true);
        webSettings.setSupportZoom(true);
        webSettings.setLoadWithOverviewMode(true);


        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        activity_web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
        });*/
        // 覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        activity_web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);

                return true;
            }

            // 网页加载结束
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                activity_web.getSettings().setJavaScriptEnabled(true);
                // web 页面加载完成，添加监听图片的点击 js 函数
                activity_web.setImageClickListner();
                //解析 HTML
                activity_web.parseHTML(view);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(DesignerBrandStoryActivity.this, "请检查您的网络设置", Toast.LENGTH_SHORT).show();
            }

        });

    }

    @Override
    protected void initListener() {
        registerClickListener(btn_title_right);
    }

    @Override
    protected void initData() {
        activity_web.clearImgs();
        loginState = SharePreferencesHelper.getInstance(this).getBoolean("loginState");
        token = SharePreferencesHelper.getInstance(this).getString("token");
        userId = SharePreferencesHelper.getInstance(this).getString("userId");
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        String htmlPath = intent.getStringExtra("htmlPath");
        nickName = intent.getStringExtra("nickName");
        headImg = intent.getStringExtra("headImg");
        String brandName = intent.getStringExtra("brandName");
        String signatures = intent.getStringExtra("signature");
        String coverImg = intent.getStringExtra("coverImg");
        dUserId = intent.getStringExtra("dUserId");
        activity_web.loadUrl(htmlPath);
        tvTitle.setText(name);
        //头像地址
        if (EmptyUtils.isNotEmpty(headImg)) {
            ImageUtils.loadImageView(DesignerBrandStoryActivity.this, headImg, head_img);

        } else {
            ImageUtils.loadImageView(DesignerBrandStoryActivity.this, R.drawable.symbols_bitmap_head_portrait, head_img);

        }
        if (EmptyUtils.isNotEmpty(coverImg)) {
            ImageUtils.loadImageView(DesignerBrandStoryActivity.this, coverImg, img_designer);

        } else {
            ImageUtils.loadImageView(DesignerBrandStoryActivity.this, R.drawable.img_bitmap_designer, img_designer);

        }
        nick_name.setText(nickName);
        brand_name.setText(brandName);
        signature.setText(signatures);

    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.btn_title_right:
                /*分享*/
                ShowPickDialog();
                break;
            default:
                break;
        }

    }

    //弹出分享框
    private void ShowPickDialog() {
        if (selectImage == null) {
            selectImage = new ShareMenuPopupWindow(this);
            selectImage.setOnShareClickListener(shareClickListener);
        }
        selectImage.showAtLocation(this.findViewById(R.id.linearlayout_shop), Gravity.BOTTOM, 0, 0);
    }

    private ShareMenuPopupWindow.OnShareClickListener shareClickListener = new ShareMenuPopupWindow.OnShareClickListener() {

        @Override
        public void onShareClick(int position) {
            screenshot(position);
        }
    };

    private void share() {
        ShareAction shareAction = new ShareAction(this);
        UMImage thumb = new UMImage(this, headImg);
        UMWeb web = new UMWeb("http://yididesign.cn/share_user_story.html?designerId=" + dUserId);
        web.setTitle(nickName + "的品牌故事");//标题
        web.setThumb(thumb);  //缩略图
        web.setDescription("来艺滴，看更多故事");//描述
        if (sharePosition == 1) {
            shareAction.setCallback(umShareListener).withMedia(web).share();


        } else {
            shareAction.setCallback(umShareListener).withMedia(web).share();

        }
        switch (sharePosition) {

            case 0:
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN); // 微信好友
                break;
            case 1:
                shareAction.setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE);// 微信朋友圈
                break;

        }
        shareAction.share();
    }

    public void screenshot(int position) {
//        if (!LoadDialog.isShowProgressDialog()) {
//            LoadDialog.showProgressDialog(getActivity(), "制作分享数据...", false, false);
        sharePosition = position;
        share();

        //   }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
            selectImage.dismiss();
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
            if (loginState == true) {
                IntegralChangeInterface.IntegralChange(DesignerBrandStoryActivity.this, userId, token, "4", "1", "分享成功啦");
                MobclickAgent.onEvent(DesignerBrandStoryActivity.this, "DesignerBrandStoryShare", "设计师品牌故事分享");
            } else {
                ToastUtil.showToast("分享成功啦");
            }
            selectImage.dismiss();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(DesignerBrandStoryActivity.this, " 分享失败啦", Toast.LENGTH_SHORT).show();
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
            selectImage.dismiss();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(DesignerBrandStoryActivity.this, " 分享取消了", Toast.LENGTH_SHORT).show();
            selectImage.dismiss();
        }

    };
}
